#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  Copyright 2012 Leonardo

from reportlab.lib.pagesizes import A4
from geraldo import Report, ReportBand, DetailBand, SystemField, Label, ObjectValue
from geraldo.utils import cm, BAND_WIDTH, TA_CENTER, TA_RIGHT
from geraldo.generators import PDFGenerator
from geraldo.generators import TextGenerator
#from geraldo.generators.text import DEFAULT_ESCAPE_SET

#produtos = []	
#produtos.append({'codigo': '249815', 'descricao': 'ABRAC NYLON BRASFORT 100X2,5 BR C/100',
#'quantidade': 2, 'uf': 'PC', 'preco': 1.24, 'total': 2.47})
#produtos.append({'codigo': '076481', 'descricao': 'BROCA ACO RAPIDO IRWIN 5,0MM',
#'quantidade': 3, 'uf': 'PC', 'preco': 3.69, 'total': 11.07})


class Report:
	def __init__(self, produtos):
		self.produtos = produtos
	def PDF(self):
		self.report = self.ProdutosReportPDF(queryset=self.produtos)
		self.report.generate_by(PDFGenerator, filename='produtos.pdf')
	def Texto(self):
		self.report = self.ProdutosReportTexto(queryset=self.produtos)
		self.report.generate_by(TextGenerator, to_printer=True, filename='produtos.txt')
		
	class ProdutosReportPDF(Report):
		title = 'Falcão Ferragens'
		
		class band_page_header(ReportBand):
			height = 1.3*cm
			elements = [
				SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
					style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
				SystemField(expression='%(now:%d %b %Y)s - %(now:%H:%M)s', top=0.1*cm,
					width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
				
				Label(text='Codigo', top=1.3*cm, left=0.2*cm),
				Label(text='Descrição', top=1.3*cm, left=2*cm),
				Label(text='UF', top=1.3*cm, left=13*cm),
				Label(text='Quant', top=1.3*cm, left=14.5*cm),
				Label(text='Preço', top=1.3*cm, left=16*cm),
				Label(text='Total', top=1.3*cm, left=17.5*cm),
			]
			borders = {'all': True}
			
		class band_detail(DetailBand):
			height = 0.7*cm
			#margin_top = 0
			#margin_bottom = 30
			auto_expand_height = True
			Label(text='Produtos'),
			elements = [
				ObjectValue(attribute_name='codigo', top=0.5*cm , left=0.2*cm),
				ObjectValue(attribute_name='descricao', top=0.5*cm , left=2*cm, width=10.5*cm),
				ObjectValue(attribute_name='uf', top=0.5*cm , left=13*cm),
				ObjectValue(attribute_name='quantidade', top=0.5*cm , left=14.5*cm),
				ObjectValue(attribute_name='preco', top=0.5*cm , left=16*cm),
				ObjectValue(attribute_name='total', top=0.5*cm , left=17.5*cm),
			]
		
		class band_summary(ReportBand):
			height = 0.5*cm
			elements = [
				Label(text='Total:'),
				ObjectValue(expression='sum(total)', left=1*cm),
				]
			borders = {'top': True}
			
		class band_page_footer(ReportBand):
			height = 0.5*cm
			elements = [
				Label(text='Geraldo Reports', top=0.1*cm),
				SystemField(expression=u'Página %(page_number)d de %(page_count)d', top=0.1*cm,
					width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
				]
			borders = {'top': True}
	
	#Texto		
	class ProdutosReportTexto(Report):
		title = 'Falcão Ferragens'
		
		class band_page_header(ReportBand):
			height = 1.3*cm
			elements = [
				SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
					style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
				SystemField(expression='%(now:%d %b %Y)s - %(now:%H:%M)s', top=0.8*cm, left=15,
					width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
				
				Label(text='Codigo', top=1.3*cm, left=0*cm),
				Label(text='Descrição', top=1.3*cm, left=2*cm),
				Label(text='UF', top=1.3*cm, left=13*cm),
				Label(text='Quant', top=1.3*cm, left=14.5*cm),
				Label(text='Preço', top=1.3*cm, left=16*cm),
				Label(text='Total', top=1.3*cm, left=18*cm),
			]
			
		class band_detail(DetailBand):
			height = 0.5*cm
			#margin_top = 0
			#margin_bottom = 30
			auto_expand_height = True
			Label(text='Produtos'),
			elements = [
				ObjectValue(attribute_name='codigo', top=0.8*cm , left=0),
				ObjectValue(attribute_name='descricao', top=0.8*cm , left=2*cm, width=10*cm),
				ObjectValue(attribute_name='uf', top=0.8*cm , left=13*cm),
				ObjectValue(attribute_name='quantidade', top=0.8*cm , left=14.5*cm),
				ObjectValue(attribute_name='preco', top=0.8*cm , left=16*cm),
				ObjectValue(attribute_name='total', top=0.8*cm , left=18*cm),
			]
		
		class band_summary(ReportBand):
			height = 0.5*cm
			elements = [
				Label(text='Total: ', left=15.5*cm, top=1.5*cm),
				ObjectValue(expression='sum(total)', left=17.5*cm, top=1.5*cm),
				]
			
		class band_page_footer(ReportBand):
			height = 0.5*cm
			elements = [
				Label(text='Geraldo Reports', top=0.1*cm),
				SystemField(expression=u'Página %(page_number)d de %(page_count)d', top=0.1*cm,
					width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
				]
			borders = {'top': True}

#Report(produtos).Texto()
